package com.webex.ta.hydra.gui.menu;

import com.webex.ta.hydra.core.KeywordRegistry;
import com.webex.ta.hydra.util.HydraUtils;
import com.webex.ta.hydra.gui.MenuFactory;

import javax.swing.*;

/**
 * Author: Vegaz
 * Date: 2010-10-29
 * Time: 10:59:01
 */
public class NewItemMenuProvider implements DynamicMenuProvider {
    public static final int CONTROLLER = 1;
    public static final int ACTION = 1 << 1;
    public static final int ASSERTION = 1 << 2;
    public static final int PROCESSOR = 1 << 3;
    public static final int MACRO = 1 << 4;

    public static final int ALL = CONTROLLER |  ACTION  | ASSERTION | PROCESSOR | MACRO;

    DynamicMenu actions, controllers, processors, assertions, macros;
    private String actionName;
    private int mask = 0;

    public NewItemMenuProvider(String actionName, int mask) {
        this.mask = mask;
        this.actionName = actionName;
        actions = new DynamicMenu("Action", "Action");
        actions.setIcon(HydraUtils.getImage("runtime.gif"));
        actions.setProvider(new ActionMenuProvider());

        controllers = new DynamicMenu("Controller", "Controller");
        controllers.setIcon(HydraUtils.getImage("folder.gif"));
        controllers.setProvider(new ControllerMenuProvider());

        processors = new DynamicMenu("Processor", "Processor");
        processors.setIcon(HydraUtils.getImage("processor.gif"));
        processors.setProvider(new ProcessorMenuProvider());

        assertions = new DynamicMenu("Assertion", "Assertion");
        assertions.setIcon(HydraUtils.getImage("assert.gif"));
        assertions.setProvider(new AssertionMenuProvider());

        macros = new DynamicMenu("Macro", "Macro");
        macros.setIcon(HydraUtils.getImage("macro.gif"));
        macros.setProvider(new MacroMenuProvider());
    }

    public void update(JMenu menu) {
        if ((mask & CONTROLLER) != 0)
            menu.add(controllers);
        if ((mask & ACTION) != 0)
            menu.add(actions);
        if ((mask & ASSERTION) != 0)
            menu.add(assertions);
        if ((mask & PROCESSOR) != 0)
            menu.add(processors);
        if ((mask & MACRO) != 0)
            menu.add(macros);
    }

    public boolean updateEveryTime() {
        return true;
    }

    private class ActionMenuProvider implements DynamicMenuProvider {
        public void update(JMenu menu) {
            for (String action : KeywordRegistry.getInstance().getActionNames()) {
                menu.add(MenuFactory.makeMenuItem(action, action, NewItemMenuProvider.this.actionName));
            }
        }

        public boolean updateEveryTime() {
            return true;
        }
    }

    private class ControllerMenuProvider implements DynamicMenuProvider {
        public void update(JMenu menu) {
            for (String controller : KeywordRegistry.getInstance().getControllerNames()) {
                menu.add(MenuFactory.makeMenuItem(controller, controller, NewItemMenuProvider.this.actionName));
            }
        }

        public boolean updateEveryTime() {
            return true;
        }
    }

    private class ProcessorMenuProvider implements DynamicMenuProvider {
        public void update(JMenu menu) {
            for (String processor : KeywordRegistry.getInstance().getProcessorNames()) {
                menu.add(MenuFactory.makeMenuItem(processor, processor, NewItemMenuProvider.this.actionName));
            }
        }

        public boolean updateEveryTime() {
            return true;
        }
    }

    private class AssertionMenuProvider implements DynamicMenuProvider {
        public void update(JMenu menu) {
            for (String assertion : KeywordRegistry.getInstance().getAssertionNames()) {
                menu.add(MenuFactory.makeMenuItem(assertion, assertion, NewItemMenuProvider.this.actionName));
            }
        }

        public boolean updateEveryTime() {
            return true;
        }
    }

    private class MacroMenuProvider implements DynamicMenuProvider {
        public void update(JMenu menu) {
            for (String macro : KeywordRegistry.getInstance().getMacroNames()) {
                menu.add(MenuFactory.makeMenuItem(macro, macro, NewItemMenuProvider.this.actionName));
            }
        }

        public boolean updateEveryTime() {
            return true;
        }
    }
}
